package com.vichat.common.contorller;

import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.context.ContextHolder;
import com.vichat.common.service.IExcelParser;
import com.vichat.common.vo.Json;
import com.vichat.common.vo.excel.ExcelDataWrapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/excel")
public class ExcelImportContorller {

    @Autowired
    private HttpServletRequest request;

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @RequestMapping("/excelImport")
    public String excelImportPage() {
        return "/excel/excelImport";
    }

    @RequestMapping("/getExcelInfo")
    @ResponseBody
    public Json getExcelInfo(String className, long userType) {
        Json json = new Json();
        try {
            IExcelParser excelParser = (IExcelParser) getExcelParser(className);
            Map excelInfo = excelParser.getExcelInfo(userType);
            json.setSuccess(true);
            json.setObj(excelInfo);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }

//    @RequestMapping("/doExcelImport")
//    @ResponseBody
//    public Json doExcelImport(@RequestParam("file") MultipartFile file, String className, String typeTag) {
//        Json json = new Json();
//        try {
//            IExcelParser excelParser = (IExcelParser) getExcelParser(className);
//            ExcelDataWrapper excelDataWrapper = new ExcelDataWrapper();
//            if (StringUtils.isNotBlank(typeTag) && GlobalConstant.DEVICE_TYPE_4 == Long.valueOf(typeTag)) {
//                //导入设备
//                excelDataWrapper = excelParser.parseDevice(file, request, typeTag);
//            } else if (StringUtils.isNotBlank(typeTag) && GlobalConstant.CAN_PHONE == Long.valueOf(typeTag)) {
//                //导入白名单
//                excelDataWrapper = excelParser.parseCanPhone(file, request, typeTag);
//            } else if (StringUtils.isNotBlank(typeTag) && GlobalConstant.FAMILY_PHONE == Long.valueOf(typeTag)) {
//                //导入亲情号码
//                excelDataWrapper = excelParser.parseFamPhone(file, request, typeTag);
//            } else if (StringUtils.isNotBlank(typeTag) && GlobalConstant.CLOUD_USER == Long.valueOf(typeTag)) {
//                //导入用户管理
//                excelDataWrapper = excelParser.parseUser(file, request, typeTag);
//            } else {
//                excelDataWrapper = excelParser.parse(file, request, typeTag);
//            }
//            String sessionId = request.getSession().getId();
//            excelParser.put2Cache(excelDataWrapper, sessionId);
//            json.setSuccess(true);
//            json.setMsg("导入成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            json.setMsg(e.getMessage());
//        }
//        return json;
//    }

    private IExcelParser getExcelParser(String parserClassName) {
        Map<String, IExcelParser> map = ContextHolder.getApplicationContext().getBeansOfType(IExcelParser.class, true, true);
        for (IExcelParser parser : map.values()) {
            String className = parser.getClass().getName();
            if (parserClassName != null && parserClassName.equals(className)) {
                return parser;
            }
        }
        return null;
    }

    @RequestMapping("/getImportDataGrid")
    @ResponseBody
    public Json getImportDataGrid(String className, String typeTag) {
        Json json = new Json();
        try {
            Map dataMap = new HashMap();
            IExcelParser excelParser = (IExcelParser) getExcelParser(className);
            dataMap.put("columns", excelParser.getDataGridColumnsDef(typeTag));
            String sessionId = request.getSession().getId();
            dataMap.put("errData", excelParser.getErrorList(sessionId));
            dataMap.put("data", excelParser.getEntityList(sessionId));
            json.setObj(dataMap);
            json.setSuccess(true);
            json.setMsg("导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }

    @RequestMapping("/putCacheDataIntoDB")
    @ResponseBody
    public Json putCacheDataIntoDB(String className, String typeTag) {
        Json json = new Json();
        try {
            IExcelParser excelParser = (IExcelParser) getExcelParser(className);
            String sessionId = request.getSession().getId();
            ExcelDataWrapper excelDataWrapper = excelParser.getExcelDataWrapperCache(sessionId);
            int number = excelParser.execute(excelDataWrapper, sessionId, typeTag);
            json.setSuccess(true);
            json.setMsg("入库成功，共导入" + number + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
        }
        return json;
    }

    @RequestMapping("/excelComplete")
    public String excelComplete() {
        return "/excel/excelComplete";
    }

}
